<template>
    <div class="login_container">
        <div class="login_box">
            <!-- 头像区域 -->
            <div class="avatar_box">
                <img src="../assets/logo.png" />
            </div>
            <!-- 登录表单区域 -->
            <el-form :model="loginForm" :rules="loginRormRules" class="login_form" ref="loginFormRef">
                <el-form-item prop="adminCount">
                    <el-input prefix-icon="iconfont icon-zhanghao" v-model="loginForm.adminCount"></el-input>
                </el-form-item>
                <el-form-item prop="adminPwd">
                    <el-input prefix-icon="iconfont icon-tianchongxing-" v-model="loginForm.adminPwd"
                        type="password"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="onSubmit">登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>

import { login } from '../api/admin/admin'
import { setToken } from '../utils/storage.js'
export default {
    data() {
        return {
            //表单数据
            loginForm: {
                adminCount: 'admin111',
                adminPwd: '123456'
            },
            //字段规则
            loginRormRules: {
                adminCount: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                adminPwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                ]
            }
        }
    },
    methods: {
        //登录按钮
        onSubmit() {
            this.$refs.loginFormRef.validate(valid => {
                if (valid) {
                    login(this.loginForm).then(res => {
                        if (res.data.status === 0) {
                            setToken(res.data.token)
                            this.$message({
                                message: res.data.message,
                                type: 'success'
                            });
                            this.$router.push('/home')
                        } else {
                            this.$message.error(res.data.message)
                        }
                    }).catch(err => {
                        console.log('登录错误' + err)
                    })
                } else {
                    this.$message.error('输入有误~');
                }
            })
        },
        //重置按钮
        resetLoginForm() {
            this.$refs.loginFormRef.resetFields()
        }
    }
}
</script>

<style lang="less" scoped>
.login_container {
    background-color: #2b4b6b;
    height: 100%;

    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);

        .avatar_box {
            height: 130px;
            width: 130px;
            overflow: hidden;
            border-radius: 50%;
            border: 1px solid #eee;
            padding: 10px;
            box-shadow: 0 0 10px #ddd;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;

            img {
                width: 100%;
                height: 100%;
                border-radius: 50%;
                background-color: #eee;
            }
        }

        .login_form {
            position: absolute;
            bottom: 0;
            width: 100%;
            padding: 0 20px;
            box-sizing: border-box;

            .btns {
                display: flex;
                justify-content: flex-end;
            }
        }

    }
}
</style>